#include <iostream>
#include <dlfcn.h> 
#include "dltest.h"
using namespace std;

typedef int(*theFuncPtr)(int);

theFuncPtr getFuncHandle(char* sopath)
{
	theFuncPtr dfunc = NULL;
	void* dhandle = dlopen(sopath,RTLD_NOW|RTLD_GLOBAL);
	if(NULL == dhandle)
	{
		cout<<"fail to load so"<<sopath<<endl;
		return NULL;
	}
	dfunc = (theFuncPtr)dlsym(dhandle, "dltestfoo");
	if(NULL == dfunc)
	{
		cout<<"fail to find sym dltestfoo"<<endl;
		return NULL;
	}
	return dfunc;
}

int main()
{
	int foore;
	theFuncPtr dfunc = NULL;
	char* sopath = "./dltest.so";
	sopath = "";
	dfunc = getFuncHandle(sopath);
	if(NULL == dfunc)
	{
		return 0;
	}

	foore = (*dfunc)(3);
	cout<<"foore="<<foore<<endl;
	foore = (*dfunc)(-3);
	cout<<"foore="<<foore<<endl;
	foore = (*dfunc)(0);
	cout<<"foore="<<foore<<endl;
	
	sopath = "./dltest2.so";
	dfunc = getFuncHandle(sopath);

	foore = (*dfunc)(3);
	cout<<"foore="<<foore<<endl;
	foore = (*dfunc)(-3);
	cout<<"foore="<<foore<<endl;
	foore = (*dfunc)(0);
	cout<<"foore="<<foore<<endl;

	return 0;

}

/*
int main_()
{
	int foore;
	foore = dltestfoo(3);
	cout<<"foore="<<foore<<endl;
	foore = dltestfoo(-3);
	cout<<"foore="<<foore<<endl;
	foore = dltestfoo(0);
	cout<<"foore="<<foore<<endl;
	return 0;
}
*/
